/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


//fungsi untuk mengupdate
var username = "anis";
window.friend="";
//register timer
window.timerArray= new Array();
//register yang sedang aktif
//nama friend atau room yang sedang aktif
window.activeFriendOrRoom=" ";
//tipe chat yang sedak aktif, apakah room atau friend
window.tipeActive=" ";
//
window.onload = update(username);
//window.onload = updateChatTab(username);

function getxmlhttp()
{
    try {
        //If the javascript version is greater than 5.
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        //If not, then use the older active x object.
        try {
            //If we are using Internet Explorer.
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            //Else we must be using a non-IE browser.
            xmlhttp = false;
        }
    }
    //If we are using a non-IE browser, create a javascript instance of the object.
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function processajax(serverpage,object,getorpost,str,readyfunction)
{
    var obj = document.getElementById(object);
    var xmlhttp = getxmlhttp();
    
    if(getorpost == "get"){
        xmlhttp.open("GET",serverpage);
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                if(readyfunction != null){
                    readyfunction(obj,xmlhttp.responseText);
                }
                else
                    obj.innerHTML = xmlhttp.responseText;
            }
            else{
                 standardFunction(xmlhttp, object);
            }
        }
        xmlhttp.send(null);
    }
    else{
        xmlhttp.open("POST", serverpage, true);
        //alert(serverpage);
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF8");
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                obj.innerHTML = xmlhttp.responseText;
            }
            else{
                 standardFunction(xmlhttp, object);
            }
        }
        xmlhttp.send(str);
    }
}

function standardFunction(xmlhttp,object){
//do nothing        
}

function update(username){   
    processajax("functions/process.jsp?task=updateFriends", "friends", "get", null);
    setTimeout('update("'+username+'");', 5000);
}

function getformvalue(fobj)
{
    var str="";
    var i=0;
    while((i<fobj.elements.length))
    {
        str += fobj.elements[i].name +"="+escape(fobj.elements[i].value)+"&";
        i++;
    }
    return str;
}

function submitform(theform,serverpage,objid,evt){
    var str=getformvalue(theform);
    //alert('masuksini');
    //alert(str);
    start = true;
    processajax(serverpage, objid, "post", str,evt);

}

function enterpressed(e,theform){
    var charCode;

    if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    if(charCode == 13) {
        handleSubmit(theform);
        document.getElementById("chat_input").value="";
    }
}

function handleSubmit(theform){
    var str = document.getElementById("chat_input").value;
    var objid,tipe;
    if(str.length>2 && str[0]=='/'){
       var str2 = str.substr(1, str.length);
       var hasil = str.split(" ");
       if(hasil[0] == "/addfriend"){
           objid = "friends";
           tipe="command";
       }
    }
    else{
        //sedang chatting
       objid = "dump";
       tipe="chat";
    }
    
    processajax("functions/process.jsp?task=processCommand&&tipe="+tipe+"&&tipechat="+window.tipeActive+"&&nama="+window.activeFriendOrRoom, objid, "post", getformvalue(theform));
}

//chat dengan friend
function addChatTab(username){
    //cek apakah sudah ada timernya
    if(window.timerArray[username]!=null){
        return;
    }
    var doc = document.getElementById("chat_tab");
    var docno = document.getElementById("noavailable");
    if(docno!=null){
        doc.removeChild(docno);
    }
    var childnodearray = doc.childNodes;
    var jumlah = childnodearray.length;
    alert(jumlah);
    var newdiv = document.createElement("div");
    newdiv.setAttribute("class", "chattab");
    newdiv.innerHTML=username;
    doc.appendChild(newdiv);
    //pindahkan tab active ke yang sekarang
    moveChat(username, "friend");
    //daftarkan listener
    newdiv.addEventListener("click", "moveChat("+username+",friend)",true);
    //daftarkan update chat
    window.timerArray[username] = updateChat(username,"friend");
    //daftarkan yang sedang aktif
    window.activeFriendOrRoom = username;
    //daftarkan tipe yang sedang aktif
    window.tipeActive="friend";
    
}

function moveChat(username,tipe){
    if(tipe == "friend"){
        processajax("functions/process.jsp?task=moveTab&&tipe=friend&&friend=username", "chat_area", "get", null);
    }
}

function updateChatTab(username){
    processajax("functions/process.jsp?task=updatechattab&&username="+username, "chat_tab", "get", null, readyFunctionChatTab(obj, hasil))
}

function readyFunctionChatTab(obj,hasil){
    obj.appendChild(hasil);
}